/*
 * PractiseForm.cs - Wordtrainer
 * 
 * Copyright (C) 1998-2007 Markus Kilås
 *               
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 *
 * Authors:
 *   Markus Kilås <markus@markuspage.com>
 */

using System;
using System.Collections;
using Gtk;
using Mono.Unix;

namespace WtSharp
{
	
	
	public partial class PractiseForm : Gtk.Bin, IPractiseForm
	{
		private Practise practise;
		private WtSharp.SpecialCharacters m_SpecialChars;
		
		public PractiseForm(Practise practise, WtSharp.SpecialCharacters specialChars)
		{
			this.practise = practise;
			this.m_SpecialChars = specialChars;
			this.Build();
			//practise.listener = this;
			
			// Event handlers
			Answer.Entry.Activated += OnAnswerActivated;
			Answer.Entry.KeyPressEvent += OnSpecialKeyPressEvent;
			Answer.Entry.Changed += OnAnswerTextChanged;
			specialChars.ChangedEvent += OnSpecialCharsChanged;
		}
		
		public void OnSpecialCharsChanged(object source)
		{
			PractiseSpecial1.Label = m_SpecialChars.Label1;
			PractiseSpecial2.Label = m_SpecialChars.Label2;
			PractiseSpecial3.Label = m_SpecialChars.Label3;
			PractiseSpecial4.Label = m_SpecialChars.Label4;
		}
	
		/*****************************  PractiseGUI ****************************/
		
		public void ClearAnswerBox()
		{
			Answer.Entry.Text = "";
		}
		
		public void FillAnswerAlternatives(ArrayList alternatives)
		{
			// ComboBox
			Answer.Clear();
	        CellRendererText cell = new CellRendererText();
	        Answer.PackStart(cell, false);
	        Answer.AddAttribute(cell, "text", 0);
	        ListStore store = new ListStore(typeof (string));
	        Answer.Model = store;
			foreach(string word in alternatives)
			{
				Answer.AppendText(word);
			}
		}
		
		public void UpdateInfo()
		{
			PractiseLabelQuestion.Text = ""+(practise.m_nPractiseIndex+1);
			PractiseLabelWords.Text = ""+practise.m_PractiseInfo.WordCount;
			PractiseLabelRight.Text = ""+practise.m_PractiseInfo.Right;
			PractiseLabelWrong.Text = ""+practise.m_PractiseInfo.Wrong;
			PractiseLabelHow.Text = practise.m_PractiseInfo.PractiseF ? Catalog.GetString("Foreign word") : Catalog.GetString("Translation");
		}
		
		public void SetQuestion(string question, Gdk.Pixbuf pixbuf)
		{
			Question.Text = question;
		
			imageQuestion.Pixbuf = pixbuf;
			Answer.Entry.Text = "";
			PractiseOK.Label = Catalog.GetString("OK");
			PractiseOK.Sensitive = true;
			Answer.GrabFocus();
		}
		
		public void SetPractseOKLabel(string label)
		{
			PractiseOK.Label = label;
		}
		
		public void DisableInput(bool disable)
		{
			PractiseOK.Sensitive = !disable;
		}
		
		public void SetLabelAnswerMarkup(string markup)
		{
			labelAnswer.Markup = markup; 
		}
		
		public void SetAnswerEntryBaseColor(Gdk.Color color)
		{
			Answer.Entry.ModifyBase(StateType.Normal, color);
		}
		
		public void SetAnswerEntryText(string text)
		{
			Answer.Entry.Text = text;
		}
		
		public void AnswerGrabFocus()
		{
			Answer.Entry.GrabFocus();
		}
		
		/****************************** GUI methods ***********************/
		
		protected void OnAnswerActivated(object o, System.EventArgs args)
		{
			practise.Correct(Answer.Entry.Text);
		}
		
		protected virtual void OnPractiseOKClicked (object sender, System.EventArgs e)
		{
			practise.Correct(Answer.Entry.Text);
		}
		
		protected virtual void OnAnswerTextChanged(object sender, System.EventArgs e)
		{	
			practise.TextChanged(Answer.Entry.Text);
		}
		
		protected virtual void OnAnswerChanged (object sender, System.EventArgs e)
		{
			Answer.Entry.GrabFocus();
		}

		protected virtual void OnSpecialKeyPressEvent (object o, Gtk.KeyPressEventArgs args)
		{	
			m_SpecialChars.SpecialKeyPressed(o, args);
		}

		protected virtual void OnPractiseSpecial1Clicked (object sender, System.EventArgs e)
		{
			m_SpecialChars.MessageBox1();
			Answer.Entry.GrabFocus();
		}

		protected virtual void OnPractiseSpecial2Clicked (object sender, System.EventArgs e)
		{
			m_SpecialChars.MessageBox2();
			Answer.Entry.GrabFocus();
		}

		protected virtual void OnPractiseSpecial3Clicked (object sender, System.EventArgs e)
		{
			m_SpecialChars.MessageBox3();
			Answer.Entry.GrabFocus();
		}

		protected virtual void OnPractiseSpecial4Clicked (object sender, System.EventArgs e)
		{
			m_SpecialChars.MessageBox4();
			Answer.Entry.GrabFocus();
		}
		
	}
}
